/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"errors"
	wxcard "github.com/chanxuehong/wechat/mp/card"
	wxcardmch "github.com/chanxuehong/wechat/mp/card/submerchant"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
	. "saas_sys_base/modules/wechat/models"
)

type WxCardMchService struct {
	// add your model here
	tbWxCardMch     WxCardMch
	wechatSdkClient WechatSdkClient
}

func (t *WxCardMchService) ListWxCardMch(keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_wx_card_mch", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			//{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *WxCardMchService) GetWxCardMchById(id int64) *WxCardMch {
	return t.tbWxCardMch.RecodeById(id)
}

/* 添加
 * -----------------
 */
func (t *WxCardMchService) SaveWxCardMch(item2add *WxCardMch) bool {
	return t.tbWxCardMch.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *WxCardMchService) DelWxCardMchById(id int64) bool {
	return t.tbWxCardMch.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *WxCardMchService) UpdateWxCardMch(rc *WxCardMch) bool {
	return t.tbWxCardMch.UpdateRecode(rc)
}

func (t *WxCardMchService) UpdateRecodeByWeappIdAndMchId(rc *WxCardMch, weappid string, mchId int64) bool {
	return t.tbWxCardMch.UpdateRecodeByWeappIdAndMchId(rc, weappid, mchId)
}

func (t *WxCardMchService) GetWxCardMchByTid(tid int64) *WxCardMch {
	WxCardMch := t.tbWxCardMch.RecodeByTid(tid)
	return WxCardMch
}

func (t *WxCardMchService) CreateCardMch(appWho common.Who, tenantId int64, paraDto *CardMchInfoDto) (*wxcardmch.SubmitSubmerchantResp, error) {
	mchclient, err := t.wechatSdkClient.GetMpClientByWho(appWho)
	if err == nil {
		return nil, err
	}

	submitPara := new(wxcardmch.SubmitSubmerchantPara)
	submitPara.Info.BrandName = paraDto.BrandName
	submitPara.Info.AppId = paraDto.AppId
	submitPara.Info.LogoUrl = paraDto.LogoUrl

	submitPara.Info.EndTime = paraDto.EndTime
	submitPara.Info.PrimaryCategoryId = paraDto.PrimaryCategoryId
	submitPara.Info.SecondaryCategoryId = paraDto.SecondaryCategoryId

	//上传资质资料并配置对应请求消息参数的media id值

	/*TODO 这里改为调用接口上传素材到微信submitPara.Info.Protocol = paraDto.ProtocolId
	submitPara.Info.AgreementMediaId = paraDto.AgreementMediaId
	submitPara.Info.OperatorMediaId = paraDto.OperatorMediaId*/

	resp, err := wxcardmch.Submit(mchclient, submitPara)
	if err == nil {
		return nil, err
	}
	/*
		item2add := new(WxCardMch)
		item2add.TenantId = tenantId
		item2add.AppId = resp.Info.AppId
		item2add.TenantId = resp.Info.
		item2add. = resp.Info.

		isSuccess := t.tbWxCardMch.AddRecode(item2add)
		if !isSuccess {
			return nil, errors.New("CreateCardMch failed when AddRecode")
		}
	*/
	cardmch := NewWxCardMch(tenantId, mchclient.GetAppId(), resp.Info.MerchantId, paraDto)
	cardmch.SyncFromResp(resp) //同步返回的信息
	t.tbWxCardMch.AddRecode(cardmch)
	return resp, nil
}

func (t *WxCardMchService) GetApplyProtocol(appWho common.Who, tenantId int64, paraDto *CardMchInfoDto) ([]wxcard.CardCategory, error) {
	mchclient, err := t.wechatSdkClient.GetMpClientByWho(appWho)
	if err == nil {
		return nil, err
	}

	resp, err := wxcard.GetApplyProtocolInfo(mchclient)
	return resp, nil
}

func (t *WxCardMchService) GetCardMchList(appWho common.Who, queryPara *wxcardmch.CardMchBatchGetPara) (*wxcardmch.CardMchBatchGetResp, error) {
	mchclient, err := t.wechatSdkClient.GetMpClientByWho(appWho)
	if err == nil {
		return nil, err
	}

	resp, err := wxcardmch.BatchGet(mchclient, queryPara)
	return resp, nil
}

func (t *WxCardMchService) GetWxCardMchDetailByTid(appWho common.Who, tid int64) (*wxcardmch.SubMerchantInfo, error) {
	mchclient, err := t.wechatSdkClient.GetMpClientByWho(appWho)
	if err == nil {
		return nil, err
	}

	WxCardMch := t.tbWxCardMch.RecodeByTid(tid)
	if WxCardMch == nil {
		return nil, errors.New("未开通卡券功能")
	}

	resp, err := wxcardmch.Get(mchclient, WxCardMch.CardMchId)
	return resp, err
}
